<?php

/**
 * @user magein
 * @date 2024/1/23 10:44
 */

namespace seecms\controller;

use seecms\db\SeeDbException;
use seecms\See;
use seecms\lib\Auth;
use seecms\lib\Operate;
use seecms\SeeLang;
use seecms\lib\response\JsonResponse;

class Profile extends SeeBaseController
{
    /**
     * @return string
     */
    public function index(): string
    {
        $userinfo = See::table()->user->where('id', Auth::user()->uuid())->find();

        return $this->view('profile/index', [
            'userinfo' => $userinfo,
            'permission' => [
                'roles' => Auth::user()->role()
            ]
        ]);
    }

    /**
     * @return JsonResponse
     * @throws SeeDbException
     */
    public function login(): JsonResponse
    {
        $data = See::table()->user_login->where('user_id', Auth::user()->uuid())->order('id desc')->paginate();

        if ($data) {
            foreach ($data['data'] as &$item) {
                $item['status_text'] = $item['status'] ? SeeLang::login('success') : SeeLang::login('fail');
                $item['is_mobile_text'] = $item['is_mobile'] ? SeeLang::switch('yes') : SeeLang::switch('no');
            }
            unset($item);
        }

        return JsonResponse::success($data);
    }

    public function avatar(): JsonResponse
    {
        $result = Auth::user()->setAvatar();

        return JsonResponse::result($result);
    }

    /**
     * @return JsonResponse
     */
    public function setPassword(): JsonResponse
    {
        $result = Auth::user()->setPassword();

        return JsonResponse::result($result);
    }

    public function verifyPassword(): JsonResponse
    {
        $result = Auth::user()->verifyPassword();

        return JsonResponse::result($result);
    }

    /**
     * @return JsonResponse
     */
    public function updateInfo(): JsonResponse
    {
        $result = Auth::user()->updateInfo();

        return JsonResponse::result($result);
    }
}